<?php
/**
 * Created by PhpStorm.
 * User: 曹乐清
 * Date: 2017/3/24
 * Time: 10:32
 */

namespace Api\Controller;


use Think\Controller;

class OrderController extends Controller
{
    //@todo 生成订单
    public function createOrder(){
        if(!empty($_FILES['name'])) {
            $import=new \Think\Upload(C('UserPhoto'));//实例化上传类
            $info=$import->upload();//图片上传操作
            if(!$info){
                echo 'false';exit;
            }
            $user_id=$_POST['user_id'];
            $decorate_id=$_POST['decorate_id'];
            $product_id=$_POST['product_id'];
            $pic=$info['name']['savepath'].$info['name']['savename'];
            $data=array(
                'user_id'=>$user_id,
                'photo'=>$pic,
                'product_id'=>$product_id,
                'decorate_id'=>$decorate_id
            );
            $id=D('create_order')->add($data);
            if(!$id){
               echo 'false'; exit;
            }
           echo  'true';exit;
        }
    }
    //@todo 查询订单
    public function getUserList(){
        $post=json_decode(file_get_contents('php://input'),true);
        $user_id=$post['user_id'];
        $info=D('create_order')->where(array('user_id'=>$user_id))->limit($post['page'],5)->select();
        foreach($info as $key =>$value){
            $info[$key]['photo']='https://'.$_SERVER['SERVER_NAME'].'/Uploads/Cms/UserPhoto/'.$value['photo'];
            if($value['product_id'] == '0'){
                $info[$key]['product_thumb']='';
            }else{
                $res=D('product_pic')->find($value['product_id']);
                $info[$key]['product_thumb']='https://'.$_SERVER['SERVER_NAME'].'/Uploads/Cms/productPic/'.$res['thumb'];
            }
            if($value['decorate_id'] =='0'){
                $info[$key]['decorate_thumb']='';
            }else{
                $res=D('product_next_pic')->find($value['decorate_id']);
                $info[$key]['decorate_thumb']='https://'.$_SERVER['SERVER_NAME'].'/Uploads/Cms/productPic/'.$res['thumb'];
            }
        }
        $this->ajaxReturn(array('mate'=>array('code'=>0,'message'=>'调用成功'),'list'=>$info));
    }

    //@todo 删除单个订单
    public function delThis(){
        $post=json_decode(file_get_contents('php://input'),true);
        $id=$post['id'];
        $del=D('create_order')->delete($id);
        if(!is_numeric($del)){
            $this->ajaxReturn(array('meat'=>array('code'=>'0003','message'=>'服务器发生错误,请稍后再试')));
        }
        $this->ajaxReturn(array('meat'=>array('code'=>'0','message'=>'调用成功')));
    }

    //@todo 获取相册相框规格
    public function getPhotoBox(){
        $xk_list=D('photo_box')->where(array('type'=>1))->select();
        $xk_list=array_merge_recursive(array(array('id'=>0,'text'=>'不选择','price'=>0,'type'=>'1')),$xk_list);
        $xc_list=D('photo_box')->where(array('type'=>2))->select();
        $xc_list=array_merge_recursive(array(array('id'=>0,'text'=>'不选择','price'=>0,'type'=>'2')),$xc_list);
        $xk=array_column($xk_list,'text');
        $xc=array_column($xc_list,'text');
        $this->ajaxReturn(array('meat'=>array('code'=>0,'message'=>'调用成功'),'xk'=>$xk,'xc'=>$xc,'xk_list'=>$xk_list,'xc_list'=>$xc_list));
    }

    //@todo 查看详情
    public function getShowOrder(){
        $post=json_decode(file_get_contents('php://input'),true);
        $type=$post['type_'];
        $id=$post['id'];
        if($type=='product'){
            $info=D('product_pic')->where(array('id'=>$id))->select();
        }else{
            $info=D('product_next_pic')->where(array('id'=>array('in',explode(',',$id))))->select();
        }
        foreach($info as $key=>$value){
            $info[$key]['pic']="https://".$_SERVER['SERVER_NAME'].'/Uploads/Cms/productPic/'.$value['pic'];
        }
        $this->ajaxReturn(array('meat'=>array('code'=>0,'message'=>'调用成功'),'info'=>$info));
    }

}